package com.a918.util;

import java.util.Arrays;
import java.util.Collections;

public class shangquanF {
    public static void main(String[] args) {
        double wangdian[][] = {{100,90,100,84,90,100,100,100,100},
                {100,100,78.6,100,90,100,100,100,100},
                {75,100,85.7,100,90,100,100,100,100},
                {100,100,78.6,100,90,100,94.4,100,100},
                {100,90,100,100,100,90,100,100,80},
                {100,100,100,100,90,100,100,85.7,100},
                {100,100,78.6,100,90,100,55.6,100,100},
                {87.5,100,85.7,100,100,100,100,100,100},
                {100,100,92.9,100,80,100,100,100,100},
                {100,90,100,100,100,100,100,100,100},
                {100,100,92.9,100,90,100,100,100,100}
        };

        for (int i = 0; i < shangquan(wangdian).length; i++) {
            System.out.println(shangquan(wangdian)[i]);
        }
    }

    //输入参数为二维数组 每行代表一个网店 每列为各个网店某一指标数值
    public static Object[] shangquan(double[][] wangdian) {
        Object[] res = new Object[2];
        int n, m;
        n = wangdian.length;
        m = wangdian[0].length;

        double[][] temp = new double[2][m];
        for (int i = 0; i < m; i++) {
            temp[1][i] = 999999999;
        }
        for (int j = 0; j < m; j++) {
            double max = wangdian[0][j];
            double min = wangdian[0][j];
            for (int i = 1; i < n; i++){
                temp[0][j] = temp[0][j] > wangdian[i][j] ? temp[0][j] : wangdian[i][j];//每列最大
                temp[1][j] = temp[1][j] < wangdian[i][j] ? temp[1][j] : wangdian[i][j];//每列最小
            }
        }

        //归一化
        double guiyi[][] = new double[n][m];
        double SumCol[] = new double[m];//每列求和
        double gy;
        for (int i = 0; i < m; i++) {
            SumCol[i] = 0;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (temp[0][j] == temp[1][j]){
                    gy = 1;
                }
                else {
                    gy = (wangdian[i][j] - temp[1][j]) / (temp[0][j] - temp[1][j]);
                }
                if (gy > 0) {guiyi[i][j] = gy;}
                else {
                    guiyi[i][j] = 0;
                };
                SumCol[j] = SumCol[j] +guiyi[i][j];
            }
        }

        //Pij概率矩阵
        double P[][] = new double[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                P[i][j] = guiyi[i][j]/SumCol[j];
            }
        }

        //E信息熵矩阵
        double E[] = new double[m];
        double e;
        double SumE = 0;
        for (int i = 0; i < m; i++) {
            E[i] = 0;
        }
        for (int j = 0; j < m; j++) {
            for (int i = 0; i < n; i++){
                e = 0;
                if (P[i][j]==0) e = 0;
                else {
                    e = - P[i][j] * Math.log(P[i][j])/Math.log(n);
                }
                E[j] = E[j] + e;
                SumE = SumE + e;
            }
        }

        //W指标权重
        double W[] = new double[m];
        for (int i = 0; i < m; i++) {
            W[i] = (1-E[i])/(m-SumE);
        }

        //H综合得分
        double H[] = new double[n+m];
        for (int i = 0; i < n; i++) {
            H[i] = 0;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m-1; j++) {
                H[i] = H[i] + W[j] * guiyi[i][j];
            }
            H[i] = H[i] + W[m-1] * wangdian[i][m-1]; // 工作负荷不用再熵权
        }
        for (int i = 0; i < n; i++) {
            H[i] = H[i] * 100;
        }
        for (int i = n;i < (n+m); i++){
            H[i] = W[i-n];
        }

        res[0] = H;
        res[1] = guiyi;
        return res;
    }
}
